import logging

from db import model
from google.appengine.api import memcache
from google.appengine.ext import db
from model import Answer
from model import FriendSurvey
from model import StrainedFriendSurvey
from student_client import StudentClient


class SurveyClient(object):
  def __init__(self):
    self.student_client = StudentClient()

  def getAnswer(self, id):
    return Answer.get(Answer.gen_key(id))

  def getAnswers(self, session=None):
    answer_query = Answer.all()
    answer_query.ancestor(Answer.gen_key())
    if session:
      answer_query.filter("session = ", session)
    return answer_query.fetch(999999)

  def getSectionAnswers(self, student_id, section_id):
    answer_query = Answer.all()
    answer_query.ancestor(Answer.gen_key())
    answer_query.filter("session = ", model.CURRENT_SESSION)
    answer_query.filter("student_id = ", student_id)
    answer_query.filter("section_id = ", section_id)
    return answer_query.fetch(answer_query.count())

  def getAnswerForStudent(self, student_id, section_id, question_id):
    answer_query = Answer.all()
    answer_query.ancestor(Answer.gen_key())
    answer_query.filter("session = ", model.CURRENT_SESSION)
    answer_query.filter("student_id = ", student_id)
    answer_query.filter("section_id = ", section_id)
    answer_query.filter("question_id = ", question_id)
    return answer_query.get()

  def updateAnswer(self, answer):
    answer.put()

  def deleteAnswer(self, answer):
    answer.delete()

  def getFriendSurvey(self, id):
    return FriendSurvey.get(FriendSurvey.gen_key(id))

  def getFriendSurveyForStudent(self, student_id, friend_id):
    friend = self.student_client.getStudent(friend_id)
    friend_survey_query = FriendSurvey.all()
    friend_survey_query.ancestor(FriendSurvey.gen_key())
    friend_survey_query.filter("session = ", model.CURRENT_SESSION)
    friend_survey_query.filter("student_id = ", student_id)
    friend_survey_query.filter("friend = ", friend)
    return friend_survey_query.get()

  def getFriendSurveys(self, session=None):
    friend_survey_query = FriendSurvey.all()
    friend_survey_query.ancestor(FriendSurvey.gen_key())
    if session:
      friend_survey_query.filter("session = ", session)
    return friend_survey_query.fetch(999999)

  def getFriendSurveysForStudent(self, student_id):
    friend_survey_query = FriendSurvey.all()
    friend_survey_query.ancestor(FriendSurvey.gen_key())
    friend_survey_query.filter("session = ", model.CURRENT_SESSION)
    friend_survey_query.filter("student_id = ", student_id)
    friend_surveys = friend_survey_query.fetch(friend_survey_query.count())
    return friend_surveys

  def updateFriendSurvey(self, friend_survey):
    friend_survey.put()

  def updateFriendSurveys(self, friend_surveys):
    db.put(friend_surveys)

  def deleteFriendSurvey(self, friend_survey):
    friend_survey.delete()

  def getStrainedFriendSurvey(self, id):
    return StrainedFriendSurvey.get(StrainedFriendSurvey.gen_key(id))

  def getStrainedFriendSurveys(self, session=None):
    strained_friend_survey_query = StrainedFriendSurvey.all()
    strained_friend_survey_query.ancestor(StrainedFriendSurvey.gen_key())
    if session:
      strained_friend_survey_query.filter("session = ", session)
    return strained_friend_survey_query.fetch(999999)

  def getStrainedFriendSurveyForStudent(self, student_id, friend_id):
    friend = self.student_client.getStudent(friend_id)
    strained_friend_survey_query = StrainedFriendSurvey.all()
    strained_friend_survey_query.ancestor(StrainedFriendSurvey.gen_key())
    strained_friend_survey_query.filter("session = ", model.CURRENT_SESSION)
    strained_friend_survey_query.filter("student_id = ", student_id)
    strained_friend_survey_query.filter("friend = ", friend)
    return strained_friend_survey_query.get()

  def getStrainedFriendSurveysForStudent(self, student_id):
    strained_friend_survey_query = StrainedFriendSurvey.all()
    strained_friend_survey_query.ancestor(StrainedFriendSurvey.gen_key())
    strained_friend_survey_query.filter("session = ", model.CURRENT_SESSION)
    strained_friend_survey_query.filter("student_id = ", student_id)
    return strained_friend_survey_query.fetch(
        strained_friend_survey_query.count())

  def updateStrainedFriendSurvey(self, strained_friend_survey):
    strained_friend_survey.put()

  def deleteStrainedFriendSurvey(self, strained_friend_survey):
    strained_friend_survey.delete()

  def answerDictToModel(self, answer_dict):
    answer = None
    if answer_dict.has_key('id'):
      answer = self.getAnswer(answer_dict['id'])
      if not answer:
        raise Exception('Friend survey has been deleted.')
    else:
      # An id wasn't set but let's make sure this answer wasn't already created.
      answer = self.getAnswerForStudent(answer_dict['student_id'],
          answer_dict['section_id'], answer_dict['question_id'])

    if not answer:
      # Create a new answer.
      answer = Answer(Answer.gen_key())
      answer.session = model.CURRENT_SESSION

    answer.merge_from_dict(answer_dict)    
    return answer    

  def friendSurveyDictToModel(self, friend_survey_dict):
    friend_survey = None
    if friend_survey_dict.has_key('id'):
      friend_survey = self.getFriendSurvey(friend_survey_dict['id'])
      if not friend_survey:
        raise Exception('Friend survey has been deleted.')
    else:
      # An id wasn't set but let's make sure this friend survey wasn't already
      # created.
      friend_survey = self.getFriendSurveyForStudent(
          friend_survey_dict['student_id'], friend_survey_dict['friend']['id'])

    if not friend_survey:
      # Create a new friend survey.
      friend_survey = FriendSurvey(FriendSurvey.gen_key())
      friend_survey.session = model.CURRENT_SESSION
      friend_survey.friend = self.student_client.getStudent(
          friend_survey_dict['friend']['id'])

    friend_survey.merge_from_dict(friend_survey_dict)    
    return friend_survey    

  def strainedFriendSurveyDictToModel(self, strained_friend_survey_dict):
    strained_friend_survey = None
    if strained_friend_survey_dict.has_key('id'):
      strained_friend_survey = self.getStrainedFriendSurvey(
          strained_friend_survey_dict['id'])
      if not strained_friend_survey:
        raise Exception('Strained friend survey has been deleted.')
    else:
      # An id wasn't set but let's make sure this strained friend survey wasn't
      # already created.
      strained_friend_survey = self.getStrainedFriendSurveyForStudent(
          strained_friend_survey_dict['student_id'],
          strained_friend_survey_dict['friend']['id'])

    if not strained_friend_survey:
      # Create a new strained friend survey.
      strained_friend_survey = StrainedFriendSurvey(
          StrainedFriendSurvey.gen_key())
      strained_friend_survey.session = model.CURRENT_SESSION
      strained_friend_survey.friend = self.student_client.getStudent(
          strained_friend_survey_dict['friend']['id'])

    strained_friend_survey.merge_from_dict(strained_friend_survey_dict)    
    return strained_friend_survey    
